<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-select
          v-model="searchObj.provinceCode"
          placeholder="请选择省"
          @change="provinceChanged"
        >
          <el-option
            v-for="item in provinceList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select
        >
      </el-form-item
      >

      <el-form-item>
        <el-select
          v-model="searchObj.cityCode"
          placeholder="请选择市"
          @change="cityChanged"
        >
          <el-option
            v-for="item in cityList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select
        >
      </el-form-item
      >
      <el-form-item>
        <el-select
          v-model="searchObj.districtCode"
          placeholder="请选择地区"
          @change="districtChanged"
        >
          <el-option
            v-for="item in districtList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select
        >
      </el-form-item
      >
      <el-form-item>
        <el-input v-model="searchObj.hosname" placeholder="医院名称" />
      </el-form-item
      >

      <el-button
        type="primary"
        icon="el-icon-search"
        @click="getlist()"
      >查询
      </el-button>
      <el-button type="default" @click="resetData()"> 清空 </el-button>
    </el-form
    >

    <!--banner列表-->
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="序号" width="60" align="center">
        <template
          slot-scope="scope"
        >{{ (pageNum - 1) * pageSize + scope.$index + 1 }} </template
        >
      </el-table-column
      >

      <el-table-column label="医院logo">
        <template slot-scope="scope">
          <img
            :src="'data:image/jpeg;base64,' + scope.row.logoData"
            width="80"
          >
        </template
        >
      </el-table-column
      >

      <el-table-column prop="hosname" label="医院名称" />
      <el-table-column prop="param.hostype" label="等级" width="90" />
      <el-table-column prop="param.fullAddress" label="详情地址" />
      <el-table-column label="状态" width="80">
        <template
          slot-scope="scope"
        >{{ scope.row.status === 0 ? "未上线" : "已上线" }} </template
        >
      </el-table-column
      >
      <el-table-column prop="createTime" label="创建时间" />

      <el-table-column label="操作" width="230" align="center">
        <template slot-scope="scope">
          <el-button
            v-show="scope.row.status === 0"
            type="primary"
            icon="el-icon-setting"
            size="mini"
            @click="updateStatus(scope.row.id, 1)"
          >上线</el-button
          >
          <router-link :to="'/hospSet/hospital/show' + scope.row.id">
            <el-button type="primary" size="mini">详情</el-button>
          </router-link>
          <router-link :to="'/hospSet/hospital/schedule' + scope.row.hoscode">
            <el-button type="success" size="mini">排班</el-button>
          </router-link>
          <el-button
            v-show="scope.row.status === 1"
            type="warning"
            icon="el-icon-setting"
            size="mini"
            @click="updateStatus(scope.row.id, 0)"
          >下线</el-button
          >

        </template>

      </el-table-column
      >
    </el-table>

    <!--分页组件-->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center"
      layout="sizes,prev,pager,next,jumper,->,total,slot"
      @current-change="getList"
      @size-change="changeSize"
    />
  </div>
</template>
<script>
// 引入接口定义的js文件
import hospital from '@/api/hospital'
export default {
  data() {
    return {
      listLoading: true, // 数据是否正在加载
      list: [], // banner列表
      total: 0, // 数据库中的总记录数
      pageNum: 1, // 默认页码
      pageSize: 10, // 每页记录数
      searchObj: {}, // 查询表单对象
      provinceList: [],
      cityList: [],
      districtList: []
    }
  },
  created() {
    this.getlist()
    this.findprovinceList()
  },
  methods: {
    getlist(pageNum = 1) {
      this.pageNum = pageNum
      hospital
        .getHospitalList(pageNum, this.pageSize, this.searchObj)
        .then((response) => {
          // 获取列表
          this.list = response.data.content
          // 总记录数
          this.total = response.data.totalElements
          // 数据加载成功
          this.listLoading = false
        })
    },
    // 获取省份列表
    findprovinceList() {
      console.log('=>' + this.searchObj.cityCode)
      hospital.getProvinceByDictCode('Province').then((response) => {
        this.provinceList = response.data
      })
    },
    // 当省份下拉框值改变时 重新查询城市列表
    provinceChanged() {
      this.cityList = []
      this.searchObj.cityCode = undefined
      this.districtList = []
      this.searchObj.districtCode = null
      console.log('=>' + this.searchObj.cityCode)
      hospital
        .getCityByProvinceId(this.searchObj.provinceCode)
        .then((response) => {
          this.cityList = response.data
        })
    },
    // 当城市列表改变时刷新地区列表
    cityChanged() {
      this.districtList = []
      this.searchObj.districtCode = null
      hospital.getCityByProvinceId(this.searchObj.cityCode).then((response) => {
        this.districtList = response.data
      })
    },
    districtChanged() {
      hospital.getCityByProvinceId(this.searchObj.cityCode).then((response) => {
        this.districtList = response.data
      })
    },

    // 修改页面大小
    changeSize(size) {
      this.pageSize = size
      this.getlist()
    },
    // 刷新状态
    resetData() {
      this.searchObj = {}
      this.getlist()
    },
    // 修改状态
    updateStatus(id, status) {
      hospital.updateStatusById(id, status).then((response) => {
        this.getlist()
      })
    }
  }
}
</script>
